package com.hongxun.design.ui.headandbottonpanel;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.interfaces.BottomPanelCallback;
import com.hongxun.design.mytools.MyLog;


public class BottomControlPanel extends LinearLayout implements View.OnClickListener {
    private Context mContext;
    private ImageText mMsgBtn = null;
    private ImageText mContactsBtn = null;
    private ImageText mNewsBtn = null;
    private ImageText mSettingBtn = null;
    private int DEFALUT_BACKGROUND_COLOR = Color.rgb(243, 243, 243); //Color.rgb(192, 192, 192)
    private BottomPanelCallback mBottomCallback = null;


    public BottomControlPanel(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mMsgBtn = (ImageText) findViewById(R.id.btn_message);
        mContactsBtn = (ImageText) findViewById(R.id.btn_contacts);
        mNewsBtn = (ImageText) findViewById(R.id.btn_news);
        mSettingBtn = (ImageText) findViewById(R.id.btn_setting);
        setBackgroundColor(DEFALUT_BACKGROUND_COLOR);

    }

    /**
     * 初始化按钮状态*
     */
    public void initBottomPanel() {
        if (mMsgBtn != null) {
            mMsgBtn.setImage(R.mipmap.home_unchecked);
            mMsgBtn.setText("首页");
            mMsgBtn.setBackground();
            mMsgBtn.setButtonNum(0);

        }
        if (mContactsBtn != null) {
            mContactsBtn.setImage(R.mipmap.project_unchecked);
            mContactsBtn.setText("项目");
            mContactsBtn.setBackground();
            mContactsBtn.setButtonNum(0);
        }
        if (mNewsBtn != null) {
            mNewsBtn.setImage(R.mipmap.encyclopedias_unchecked);
            mNewsBtn.setText("百科");
            mNewsBtn.setBackground();
            mNewsBtn.setButtonNum(0);
        }
        if (mSettingBtn != null) {
            mSettingBtn.setImage(R.mipmap.my_unchecked);
            mSettingBtn.setText("我的");
            mSettingBtn.setBackground();
            mSettingBtn.setButtonNum(0);
        }
        setBtnListener();

    }

    private void setBtnListener() {
        int num = this.getChildCount();
        for (int i = 0; i < num; i++) {
            View v = getChildAt(i);
            if (v != null) {
                v.setOnClickListener(this);
            }
        }
    }

    public void setBottomCallback(BottomPanelCallback bottomCallback) {
        mBottomCallback = bottomCallback;
    }


    public void setCheck(String tag) {
        switch (tag) {
            case Constant.FRAGMENT_FLAG_HOME:
                onClick(mMsgBtn);
                break;
            case Constant.FRAGMENT_FLAG_PROJECT:
                onClick(mContactsBtn);
                break;
            case Constant.FRAGMENT_FLAG_ENCYCLOPEDIAS:
                onClick(mNewsBtn);
                break;
            case Constant.FRAGMENT_FLAG_MY:
                onClick(mSettingBtn);
                break;


        }
    }

    @Override
    public void onClick(View v) {
        initBottomPanel();
        int index = -1;
        switch (v.getId()) {
            case R.id.btn_message:
                index = Constant.BTN_FLAG_MESSAGE;
                mMsgBtn.setChecked(Constant.BTN_FLAG_MESSAGE);
                break;
            case R.id.btn_contacts:
                index = Constant.BTN_FLAG_CONTACTS;
                mContactsBtn.setChecked(Constant.BTN_FLAG_CONTACTS);
                break;
            case R.id.btn_news:
                index = Constant.BTN_FLAG_NEWS;
                mNewsBtn.setChecked(Constant.BTN_FLAG_NEWS);
                break;
            case R.id.btn_setting:
                index = Constant.BTN_FLAG_SETTING;
                mSettingBtn.setChecked(Constant.BTN_FLAG_SETTING);
                if (MyApp.isgetMessage) {
                    mSettingBtn.setButtonNum(1);
                } else {
                    mSettingBtn.setButtonNum(0);
                }
                break;
            default:
                break;
        }
        MyLog.L(MyApp.AppName + "index =" + index);
        MyLog.L(MyApp.AppName + "mBottomCallback =" + mBottomCallback);
        if (mBottomCallback != null) {
            mBottomCallback.onBottomPanelClick(index);
        }
    }

    public void setupdate() {
        if (mSettingBtn != null) {
            if (MyApp.isgetMessage) {
                mSettingBtn.setButtonNum(1);
            } else {
                mSettingBtn.setButtonNum(0);
            }
        }
    }

    public void defaultBtnChecked() {
        if (mMsgBtn != null) {
            mMsgBtn.setChecked(Constant.BTN_FLAG_MESSAGE);
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        layoutItems(left, top, right, bottom);
    }

    /**
     * 最左边和最右边的view由母布局的padding进行控制位置。这里需对第2、3个view的位置重新设置
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    private void layoutItems(int left, int top, int right, int bottom) {
//        int n = getChildCount();
//        if (n == 0) {
//            return;
//        }
//        int paddingLeft = getPaddingLeft();
//        int paddingRight = getPaddingRight();
//        MyLog.L(MyApp.AppName+"paddingLeft = " + paddingLeft + " paddingRight = " + paddingRight);
//        int width = right - left;
//        int height = bottom - top;
//        MyLog.L(MyApp.AppName + "width = " + width + " height = " + height);
//        int allViewWidth = 0;
//        for (int i = 0; i < n; i++) {
//            View v = getChildAt(i);
//            MyLog.L(MyApp.AppName + "v.getWidth() = " + v.getWidth());
//            allViewWidth += v.getWidth();
//        }
//        int blankWidth = (width - allViewWidth - paddingLeft - paddingRight) / (n - 1);
//        MyLog.L(MyApp.AppName + "blankV = " + blankWidth);
//
//        LayoutParams params1 = (LayoutParams) viewList.get(1).getLayoutParams();
//        params1.leftMargin = blankWidth;
//        viewList.get(1).setLayoutParams(params1);
//
//        LayoutParams params2 = (LayoutParams) viewList.get(2).getLayoutParams();
//        params2.leftMargin = blankWidth;
//        viewList.get(2).setLayoutParams(params2);
    }


}
